package com.syq.main;

import com.syq.bean.AirInfo;
import com.syq.dao.AirInfoDao;
import com.syq.impl.AirInfoImpl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        AirInfoDao airInfoDao;
        int num=-1;
        do {
            System.out.println("-------------------------欢迎使用航班管理系统----------------");
            System.out.println("1、查询所有航班");
            System.out.println("2、按时间查询航班");
            System.out.println("3、按目的地查询航班");
            System.out.println("4、删除航班");
            System.out.println("5、更新航班");
            System.out.println("0、退出");
            try {
                String s = sc.next();
                num = Integer.parseInt(s);
            } catch (Exception e) {
                System.out.println("请输入合理的数字：");
                sc = new Scanner(System.in);
                continue;
            }

            switch (num) {


                case 1: {

                    airInfoDao = new AirInfoImpl();
                    List<AirInfo> list = airInfoDao.getAllAirInfo();
                    System.out.println("编号\t航班号\t目的地\t起飞时间");
                    for( AirInfo airInfo : list) {
                        System.out.println(airInfo.toString());
                    }
                }
                break;
                case 2: {
                    airInfoDao = new AirInfoImpl();
                    System.out.println("请输入起飞时间：");
                    String time = sc.next();
                    List<AirInfo> airInfos = airInfoDao.findByTime(time);
                    if(airInfos!=null&&airInfos.size()>0 ) {
                        System.out.println("编号\t航班号\t目的地\t起飞时间");
                        for( AirInfo airInfo : airInfos) {
                            System.out.println(airInfo.toString());
                        }
                    } else
                        System.out.println("未找到航班信息");
                }
                break;
                case 3:{
                    airInfoDao = new AirInfoImpl();
                    System.out.println("请输入需要查询的目的地：");
                    String destination = sc.next();
                    List<AirInfo> airInfos = airInfoDao.findByDestination(destination);
                    if(airInfos!=null&&airInfos.size()>0 ) {
                        System.out.println("编号\t航班号\t目的地\t起飞时间");
                        for( AirInfo airInfo : airInfos) {
                            System.out.println(airInfo.toString());
                        }
                    } else
                        System.out.println("未找到航班信息");
                }
                break;
                case 4:{
                    airInfoDao = new AirInfoImpl();
                    System.out.println("请输入需要删除的航班号：");
                    String airId = sc.next();
                    AirInfo airInfo = airInfoDao.findByAirId(airId);
                    if(airInfo!=null) {
                        System.out.println("编号\t航班号\t目的地\t起飞时间");
                        System.out.println(airInfo.toString());
                        int count = airInfoDao.delete(airId);
                        if(count!=0) {

                            System.out.println("删除成功！");
                        }else
                            System.out.println("删除失败！");
                    }else {
                        System.out.println("无此航班号！");
                    }
                }
                break;
                case 5:{
                    airInfoDao = new AirInfoImpl();
                    System.out.println("请输入需要更新的航班号：");
                    String airId = sc.next();
                    AirInfo airInfo = airInfoDao.findByAirId(airId);
                    if(airInfo!=null) {
                        System.out.println("编号\t航班号\t目的地\t起飞时间");
                        System.out.println(airInfo.toString());
//                               System.out.println("请输入修改后的航班号：");
//                               String airid = sc.next();
                        System.out.println("请输入修改后的目的地：");
                        String destination = sc.next();
                        System.out.println("请输入修改后的起飞时间：");
                        String begintime = sc.next();
                        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                        Date date = null;
                        try {
                            date = simpleDateFormat.parse(begintime);
                        } catch (ParseException e) {
                            e.printStackTrace();
                        }
                        airInfo.setDestination(destination);
                        airInfo.setBegintime(date);
                        System.out.println(airInfo.toString());
                        int count = airInfoDao.update(airInfo);
                        if(count!=0) {
                            System.out.println("更新成功！");
                        }else
                            System.out.println("更新失败！");
                    }else {
                        System.out.println("无此航班号！");
                    }
                }
                break;
                case 0:
                    break;


            }





        }while (num!=0);
        System.out.println("谢谢使用！");

    }
}
